package org.urchr.web.db;


import java.io.Serializable;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class DBSession {
	
	private Session session = null;
	private Transaction transaction = null;
	
	
	public DBSession() {
		session = DBUtil.getSession();
	}
	
	public void begin(){
		if(transaction==null)
			transaction = session.beginTransaction();
	}
	
	public void commit(){
		if(transaction!=null)
			transaction.commit();
		transaction = null;
	}
	
	public void rollback(){
		if(transaction!=null){
			try {
				transaction.rollback();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		transaction = null;
	}
	
	public Session getSession(){
		return session;
	}
	
	public void save(Object o) {
		session.save(o);
	}
	
	public void close(){
		try {
			session.close();			
		} catch (Throwable e) {
		}
	}
	
	public Query createQuery(String query){
		return session.createQuery(query);
	}
	
	public Object get(Class _class, Serializable id){
		return getSession().get(_class, id);
	}
}
